package com.ljx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljx.Domain.Category;
import com.ljx.Domain.R;
import com.ljx.Domain.Setmeal;
import com.ljx.Dto.SetmealDto;
import com.ljx.Service.CategoryService;
import com.ljx.Service.SetmealDishService;
import com.ljx.Service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @Autowired
    CategoryService categoryService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "SetmealCache",allEntries=true)
    @PostMapping
    public R<String> setmealAdd(@RequestBody SetmealDto setmealDto) {
        setmealService.saveSetmeal(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐管理分页
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> setmealPage(int page, int pageSize, String name) {
        //创建page对象
        Page<Setmeal> pages = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Setmeal::getName, name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> page1 = setmealService.page(pages, wrapper);
        //复制对象
        BeanUtils.copyProperties(page1, setmealDtoPage, "records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> setmealDtoRecords = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
//            将item中的数据复制到setmealDto
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            //调用category数据层，返回类型分类名,保存到setmealDto
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoRecords);
        return R.success(setmealDtoPage);
    }

    /**
     * 套餐删除
     *
     * @param ids
     * @return
     */
    @CacheEvict(value = "SetmealCache",allEntries=true)
    @DeleteMapping
    public R<String> setmeal(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }

    /**
     * 根据categoryId查询套餐
     * @param setmeal
     * @return
     */
    @Cacheable(value = "SetmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("list")
    public R<List<Setmeal>> setmealById(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }
}
